package edu.grupo4.appeventos;

import java.util.ArrayList;

import java.util.jar.Attributes;

import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class RssHandler extends DefaultHandler {
    private ArrayList<Evento> eventos;
    private Evento eventoActual;
    private StringBuilder sbTexto;
 
    public ArrayList<Evento> getEventos(){
        return eventos;
    }
 
    @Override
    public void characters(char[] ch, int start, int length)
                   throws SAXException {
 
        super.characters(ch, start, length);
 
        if (this.eventoActual != null)
        	sbTexto.append(ch, start, length);
    }
 
    @Override
    public void endElement(String uri, String localName, String name)
                   throws SAXException {
 
        super.endElement(uri, localName, name);
 
        if (this.eventoActual != null) {        	
        
            if (localName.equals("title")) {
            	eventoActual.setTitulo(sbTexto.toString());
            } else if (localName.equals("link")) {
            	eventoActual.setLink(sbTexto.toString());
            } else if (localName.equals("description")) {
            	eventoActual.setDescripcion(sbTexto.toString());
            } else if (localName.equals("guid")) {
            	eventoActual.setGuid(sbTexto.toString());
            } else if (localName.equals("author")) {
            	eventoActual.setAutor(sbTexto.toString());
            } else if (localName.equals("pubDate")) {
            	eventoActual.setFecha(sbTexto.toString());
            } else if (localName.equals("category")) {
            	eventoActual.setCategotia(sbTexto.toString());
            } else if (localName.equals("item")) {
                eventos.add(eventoActual);
            }
 
            sbTexto.setLength(0);
        }
    }
 
    @Override
    public void startDocument() throws SAXException {
 
        super.startDocument();
 
        eventos = new ArrayList<Evento>();
        sbTexto = new StringBuilder();
    }
 
    public void startElement(String uri, String localName,String name, Attributes attributes) throws SAXException {
 
        //super.startElement(uri, localName, name, attributes);
    	super.startElement(uri, localName, name, (org.xml.sax.Attributes) attributes);
 
        if (localName.equals("item")) {
            eventoActual = new Evento();
        }
    }
}


